from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, 
                            QHBoxLayout, QPushButton, QLineEdit, QListWidget, 
                            QLabel, QMessageBox,QDialog)

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from sls_ngc_mainview import NGCMainWindow  # 替换为实际模块名

class OriginDialog(QDialog):
    def __init__(self, parent):
        super().__init__(parent=parent)
        self.resize(400, 200)
        self.parent = parent
        self.parent:NGCMainWindow
        self.parent.notifyClass.add_notify_Cordinate(self.on_click_point)
        self.origin_click_points = None
        Vlayout = QVBoxLayout()
        self.origin_label = QLabel("请在地图上选择参考点，或取消")
        self.origin_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        self.origin_label.setStyleSheet("""
            background-color: white;
            color: black;
            font-size: 16px;
            border: 1px solid #ccc;
            padding: 30px;
        """)
        Hlayout = QHBoxLayout()

        enter_button = QPushButton("确认")
        resetting_button = QPushButton("重置")
        cancel_button = QPushButton("取消")

        Hlayout.addStretch()
        Hlayout.addWidget(enter_button)
        Hlayout.addWidget(resetting_button)
        Hlayout.addWidget(cancel_button)

        Vlayout.addStretch()
        Vlayout.addWidget(self.origin_label)
        Vlayout.addStretch()
        Vlayout.addLayout(Hlayout)
        self.setLayout(Vlayout)

        enter_button.clicked.connect(self.enter_button_clicked)
        resetting_button.clicked.connect(self.resetting_button)
        cancel_button.clicked.connect(self.cancel_button)

    def on_click_point(self,latitude,longitude):
        self.origin_click_points = latitude,longitude
        self.parent.del_allmarkers()
        self.parent.add_marker(latitude,longitude,"mark")
        self.origin_label.setText(f"原点已设置\n纬度: {latitude:.6f}\n经度: {longitude:.6f}")
    def enter_button_clicked(self):
        if self.origin_click_points is not None:
            latitude,longitude = self.origin_click_points
            self.parent.statusBar_signal.emit(f"已设置原点为{latitude=}，{longitude=}")
            self.parent.del_allmarkers()
            self.parent.notifyClass.remove_notify_Cordinate(self.on_click_point)
            self.close()
        else:
            QMessageBox.warning(
                self,
                "未选择参考点",
                "请先在地图上点击选择一个参考点，或点击“取消”退出。",
                QMessageBox.StandardButton.Ok
            )
            self.origin_label.setText(f"请选点，或取消")
    def resetting_button(self):
        self.parent.del_allmarkers()
        self.origin_label.setText(f"请重新选点")
        self.parent.statusBar_signal.emit(f"请重新选点")
        self.origin_click_points = None
    def cancel_button(self):
        self.parent.statusBar_signal.emit("")
        self.parent.del_allmarkers()
        self.parent.notifyClass.remove_notify_Cordinate(self.on_click_point)
        self.close()

class GPSRectifyDialog(QDialog):
    dlat = 0.0
    dlon = 0.0
    def __init__(self, parent):
        super().__init__(parent = parent)
        self.resize(400, 200)
        self.parent = parent
        self.parent:NGCMainWindow
        self.parent.notifyClass.add_notify_Cordinate(self.on_click_point)

        GPSRectifyDialog.dlat = 0.
        GPSRectifyDialog.dlon = 0.

        self.lat_lon = []
        self.ma = 0
        self.Clabel = QLabel("请在附近区域任选一点")
        self.Clabel.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        # self.Clabel.setAlignment(QtCore.Qt.AlignmentFlag.AlignVCenter)
        self.Clabel.setStyleSheet("""
            background-color: white;
            color: black;
            font-size: 16px;
            border: 1px solid #ccc;
            padding: 30px;
        """)
        enter_button = QPushButton("确认")
        re_button = QPushButton("重新选点")
        cancel_button = QPushButton("取消")

        Hlayout = QHBoxLayout()
        Hlayout.addStretch()
        Hlayout.addWidget(enter_button)
        Hlayout.addWidget(re_button)
        Hlayout.addWidget(cancel_button)

        Vlayout = QVBoxLayout()
        Vlayout.addWidget(self.Clabel)
        Vlayout.addLayout(Hlayout)
        self.setLayout(Vlayout)

        enter_button.clicked.connect(self.enter_button_clicked)
        re_button.clicked.connect(self.re_button_clicked)
        cancel_button.clicked.connect(self.cancel_button_clicked)

    def on_click_point(self,latitude,longitude):
        self.lat_lon.append([latitude,longitude])
        if self.ma == 0:
            self.parent.add_marker(latitude,longitude,"first")
            self.Clabel.setText("请点击标识点")
            self.ma += 1 
        elif self.ma == 1:
            self.parent.del_allmarkers()
            self.Clabel.setText("请测试")
            GPSRectifyDialog.dlat = self.lat_lon[1][0]-self.lat_lon[0][0]+0.0000085
            GPSRectifyDialog.dlon = self.lat_lon[1][1]-self.lat_lon[0][1]-0.0000085
            self.ma += 1 
        else :
            self.parent.del_allmarkers()
            self.parent.add_marker(latitude,longitude,"none")
    
    def enter_button_clicked(self):
        self.lat_lon = []
        self.parent.del_allmarkers()
        self.ma = 0
        self.parent.notifyClass.remove_notify_Cordinate(self.on_click_point)
        self.close()

    def re_button_clicked(self):
        GPSRectifyDialog.dlat = 0.
        GPSRectifyDialog.dlon = 0.
        self.Clabel.setText("请点击标识点")
        self.lat_lon = []
        self.parent.del_allmarkers()
        self.ma = 0

    def cancel_button_clicked(self):
        GPSRectifyDialog.dlat = 0.
        GPSRectifyDialog.dlon = 0.
        self.parent.del_allmarkers()
        self.ma = 0
        self.parent.notifyClass.remove_notify_Cordinate(self.on_click_point)
        self.close()